<template>
  <CollapseContainer title="基本设置" :canExpan="false">
    <a-row :gutter="24">
      <a-col :span="14">
        <BasicForm @register="register" />
      </a-col>
    </a-row>
    <Button type="primary" @click="handleSubmit"> 保存 </Button>
  </CollapseContainer>
</template>
<script lang="ts">
  import { Button, Row, Col } from 'ant-design-vue';
  import { defineComponent, onMounted, inject } from 'vue';
  import { BasicForm, useForm } from '@/components/Form/index';
  import { CollapseContainer } from '@/components/Container';

  import { useMessage } from '@/hooks/web/useMessage';

  import { getConfigList, saveBasicsConfig } from '@/api/app/setting';
  import { baseSetschemas } from './data';

  export default defineComponent({
    components: {
      BasicForm,
      CollapseContainer,
      Button,
      ARow: Row,
      ACol: Col,
    },
    setup() {
      const { createMessage } = useMessage();
      const tab_id: any = inject('tab_id');

      const [register, { setFieldsValue, validate }] = useForm({
        labelWidth: 120,
        schemas: baseSetschemas,
        showActionButtonGroup: false,
      });

      onMounted(async () => {
        const data = await getConfigList(tab_id.value);
        const initialStatus = data.station_open == '1';
        // 更新 station_open 字段以控制 Switch 组件的状态
        data.station_open = initialStatus;
        setFieldsValue(data);
      });

      async function handleSubmit() {
        try {
          const data = await validate();
          await saveBasicsConfig(data).then(() => {
            createMessage.success('保存成功!');
          });
        } catch (e) {
          createMessage.success('保存失败!');
        }
      }

      return {
        register,
        handleSubmit,
      };
    },
  });
</script>

<style lang="less" scoped>
  .ant-btn-primary {
    margin-left: 120px;
  }

  .change-avatar {
    img {
      display: block;
      margin-bottom: 15px;
      border-radius: 50%;
    }
  }
</style>
./data
